package com.fau.om.action.index;

import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.om.action.PublicAction;
import com.fau.om.entity.MedicalAdmin;
import com.fau.om.entity.MedicalAlternately;
import com.opensymphony.xwork2.ModelDriven;
/**
 * @desc 业务系统与软交换交互Action
 * @author kangyj
 * @datetime 2014年12月24日,上午12:20:29
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class AlternatelyAction extends PublicAction implements ModelDriven<MedicalAlternately> {

	private static final long serialVersionUID = -4477937199758162694L;
	
	private MedicalAlternately alternately = new MedicalAlternately();

	/**
	 * @desc 查询所有队列
	 * @return
	 * @author kangyj
	 * @datetime 2015年2月15日,下午5:51:25
	 */
	@Action(value = "alternately_find_queue")
	public String findAllQueue() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getAlternatelyManagerService().findAllQueue(admin);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 绑定分机
	 * @return
	 * @author kangyj
	 * @datetime 2015年2月16日,上午10:30:05
	 */
	@Action(value = "alternately_login_ext")
	public String loginExt() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		this.baseAllService.getAlternatelyManagerService().loginExt(alternately.getUserName(), alternately.getExtension(), alternately.getQueue());
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 离席
	 * @return
	 * @author kangyj
	 * @datetime 2015年2月16日,下午12:18:26
	 */
	@Action(value = "alternately_loginout_ext")
	public String loginOutExt() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		this.baseAllService.getAlternatelyManagerService().loginOutExt(alternately.getUserName(), alternately.getExtension(), alternately.getQueue());
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询当前登录的分机和队列
	 * @return
	 * @author kangyj
	 * @datetime 2015年2月16日,下午12:39:03
	 */
	@Action(value = "alternately_find_ext_queue")
	public String findQueue() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String extQueue = this.baseAllService.getAlternatelyManagerService().findExtQueue(admin);
		if(StringUtils.isNotBlank(extQueue)) {
			String[] extQueues = extQueue.split("\\|");
			map.put("extension", extQueues[0]);
			map.put("queue", extQueues[1]);
			map.put("success", true);
		} else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询当前用户分机
	 * @return
	 * @author kangyj
	 * @datetime 2015年2月3日,上午10:43:03
	 */
	@Action(value = "alternately_cur_extension")
	public String findCurExtension() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String phoneNum = this.baseAllService.getAlternatelyManagerService().findCurExtension(admin.getId());
		map.put("phoneNum", phoneNum);
		formatJSON(response, map);
		return null;
	}

	@Override
	public MedicalAlternately getModel() {
		return alternately;
	}
	
}
